package de.hdodenhof.circleimageview.sample.ui.fragment.progress;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;


import com.app.progresviews.ProgressLine;
import com.app.progresviews.ProgressWheel;
import com.daimajia.numberprogressbar.NumberProgressBar;
import com.daimajia.numberprogressbar.OnProgressBarListener;
import com.github.ybq.android.spinkit.sprite.Sprite;
import com.github.ybq.android.spinkit.style.DoubleBounce;
import com.github.ybq.android.spinkit.style.RotatingCircle;

import java.util.Timer;
import java.util.TimerTask;

import androidx.fragment.app.Fragment;
import de.hdodenhof.circleimageview.sample.R;
import me.zhanghai.android.materialprogressbar.CircularProgressDrawable;
import me.zhanghai.android.materialprogressbar.IndeterminateHorizontalProgressDrawable;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;

public class ProgressBarFragment extends Fragment {
    NumberProgressBar mNumberProgressBar;
    ProgressWheel mProgressWheel;
    ProgressLine mProgressLine;
    Timer timer;
    ProgressBar mProgressBar;
    ImageView mImageView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_progress_bar, null);
        mNumberProgressBar = (NumberProgressBar) view.findViewById(R.id.number_progress_bar);
        mProgressWheel = view.findViewById(R.id.wheelprogress);
        mProgressLine = view.findViewById(R.id.progress_line);
        ((MaterialProgressBar)view.findViewById(R.id.material_progress_bar1)).setIndeterminateDrawable(new IndeterminateHorizontalProgressDrawable(getActivity()));
//        ((MaterialProgressBar)view.findViewById(R.id.material_progress_bar2)).setIndeterminateDrawable(new CircularProgressDrawable(0, getActivity()));
//        ((MaterialProgressBar)view.findViewById(R.id.material_progress_bar3)).setIndeterminateDrawable(new IndeterminateHorizontalProgressDrawable(getActivity()));
        mProgressBar = view.findViewById(R.id.progress_1);
        mImageView = (ImageView)view.findViewById(R.id.image_1);
        return view;
    }

    public void onResume(){
        super.onResume();
        mNumberProgressBar.setOnProgressBarListener(new OnProgressBarListener() {
            @Override
            public void onProgressChange(int current, int max) {
                if (current == max) {
                    timer.cancel();

                    Toast.makeText(getActivity(), "完成！", Toast.LENGTH_SHORT).show();
                    mNumberProgressBar.setProgress(0);
                }
            }
        });

        mNumberProgressBar.setProgress(0);
        mNumberProgressBar.setMax(100);
        Sprite doubleBounce = new DoubleBounce();
        mProgressBar.setIndeterminateDrawable(doubleBounce);

        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mNumberProgressBar.incrementProgressBy(1);

                    }
                });
            }
        }, 2000, 50);
        mProgressWheel.setPercentage(200);
        mProgressLine.setmPercentage(100);
        Sprite rotatingCircle = new RotatingCircle(){
            public void draw(Canvas c){
                super.draw(c);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hugh);
                c.drawBitmap(bitmap, 0 , 0 , new Paint());
            }
        };
        rotatingCircle.setColor(Color.BLUE);
        mImageView.setImageDrawable(rotatingCircle);
//        AlphaAnimation alphaAnimation =   new AlphaAnimation(1.0f, 0.5f);
//        alphaAnimation.setDuration(15000);
        rotatingCircle.start();
//        mImageView.setAnimation(alphaAnimation);
    }


}
